#coding=utf-8
#Boa:FramePanel:PanelProductCustHistory

import wx

from product import CDataProduct
from MyListCtrl import MyListCtrlProSaleDetail, MyListCtrlProPurDetail

[wxID_PANELPRODUCTCUSTHISTORY, wxID_PANELPRODUCTCUSTHISTORYBTNMANUALNUM, 
 wxID_PANELPRODUCTCUSTHISTORYBTN_SAVE, wxID_PANELPRODUCTCUSTHISTORYBUTTON_ENABLE, 
 wxID_PANELPRODUCTCUSTHISTORYSTATICTEXT1, wxID_PANELPRODUCTCUSTHISTORYSTATICTEXT2, 
 wxID_PANELPRODUCTCUSTHISTORYSTATICTEXT3, wxID_PANELPRODUCTCUSTHISTORYTCMANUALNUM, 
 wxID_PANELPRODUCTCUSTHISTORYTCMEMO, wxID_PANELPRODUCTCUSTHISTORYTCNAME, 
 wxID_PANELPRODUCTCUSTHISTORYTCONHAND, wxID_PANELPRODUCTCUSTHISTORYTCPOSITION, 
 wxID_PANELPRODUCTCUSTHISTORYTCSPEC, wxID_PANELPRODUCTCUSTHISTORYTMANUALNUM, 
 wxID_PANELPRODUCTCUSTHISTORYTNAME, wxID_PANELPRODUCTCUSTHISTORYTONHAND, 
] = [wx.NewId() for _init_ctrls in range(16)]

class PanelProductCustHistory(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELPRODUCTCUSTHISTORY,
              name=u'PanelProductCustHistory', parent=prnt, pos=wx.Point(166,
              235), size=wx.Size(1113, 603), style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(1097, 564))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.tName = wx.StaticText(id=wxID_PANELPRODUCTCUSTHISTORYTNAME,
              label=u'\u5546\u54c1\uff1a', name=u'tName', parent=self,
              pos=wx.Point(16, 16), size=wx.Size(51, 16), style=0)

        self.tcName = wx.TextCtrl(id=wxID_PANELPRODUCTCUSTHISTORYTCNAME,
              name=u'tcName', parent=self, pos=wx.Point(72, 8),
              size=wx.Size(272, 26), style=0, value=u'')
        self.tcName.SetEditable(True)
        self.tcName.Enable(True)

        self.tOnhand = wx.StaticText(id=wxID_PANELPRODUCTCUSTHISTORYTONHAND,
              label=u'\u5e93\u5b58\uff1a', name=u'tOnhand', parent=self,
              pos=wx.Point(368, 16), size=wx.Size(51, 16), style=0)

        self.tcOnhand = wx.TextCtrl(id=wxID_PANELPRODUCTCUSTHISTORYTCONHAND,
              name=u'tcOnhand', parent=self, pos=wx.Point(424, 8),
              size=wx.Size(100, 26), style=0, value=u'')
        self.tcOnhand.SetEditable(True)
        self.tcOnhand.Enable(True)

        self.tcSpec = wx.TextCtrl(id=wxID_PANELPRODUCTCUSTHISTORYTCSPEC,
              name=u'tcSpec', parent=self, pos=wx.Point(72, 40),
              size=wx.Size(272, 26), style=0, value=u'')
        self.tcSpec.SetEditable(True)
        self.tcSpec.Enable(True)

        self.btnManualNum = wx.Button(id=wxID_PANELPRODUCTCUSTHISTORYBTNMANUALNUM,
              label=u'\u4fee\u6539', name=u'btnManualNum', parent=self,
              pos=wx.Point(1032, 8), size=wx.Size(53, 26), style=0)
        self.btnManualNum.Bind(wx.EVT_BUTTON, self.OnBtnManualNumButton,
              id=wxID_PANELPRODUCTCUSTHISTORYBTNMANUALNUM)

        self.tcManualNum = wx.TextCtrl(id=wxID_PANELPRODUCTCUSTHISTORYTCMANUALNUM,
              name=u'tcManualNum', parent=self, pos=wx.Point(928, 8),
              size=wx.Size(100, 26), style=0, value=u'')

        self.tManualNum = wx.StaticText(id=wxID_PANELPRODUCTCUSTHISTORYTMANUALNUM,
              label=u'\u624b\u5de5\u7f16\u53f7\uff1a', name=u'tManualNum',
              parent=self, pos=wx.Point(840, 16), size=wx.Size(85, 16),
              style=0)

        self.TCPosition = wx.TextCtrl(id=wxID_PANELPRODUCTCUSTHISTORYTCPOSITION,
              name=u'TCPosition', parent=self, pos=wx.Point(424, 40),
              size=wx.Size(100, 26), style=0, value=u'')

        self.staticText1 = wx.StaticText(id=wxID_PANELPRODUCTCUSTHISTORYSTATICTEXT1,
              label=u'\u89c4\u683c', name='staticText1', parent=self,
              pos=wx.Point(16, 48), size=wx.Size(34, 16), style=0)

        self.staticText2 = wx.StaticText(id=wxID_PANELPRODUCTCUSTHISTORYSTATICTEXT2,
              label=u'\u4ed3\u4f4d\uff1a', name='staticText2', parent=self,
              pos=wx.Point(368, 48), size=wx.Size(51, 16), style=0)

        self.btn_save = wx.Button(id=wxID_PANELPRODUCTCUSTHISTORYBTN_SAVE,
              label=u'\u4fdd\u5b58\u4fee\u6539', name=u'btn_save', parent=self,
              pos=wx.Point(728, 40), size=wx.Size(93, 26), style=0)
        self.btn_save.Enable(False)
        self.btn_save.Bind(wx.EVT_BUTTON, self.OnBtn_saveButton,
              id=wxID_PANELPRODUCTCUSTHISTORYBTN_SAVE)

        self.TCMemo = wx.TextCtrl(id=wxID_PANELPRODUCTCUSTHISTORYTCMEMO,
              name=u'TCMemo', parent=self, pos=wx.Point(600, 8),
              size=wx.Size(224, 26), style=0, value=u'')

        self.staticText3 = wx.StaticText(id=wxID_PANELPRODUCTCUSTHISTORYSTATICTEXT3,
              label=u'\u5907\u6ce8\uff1a', name='staticText3', parent=self,
              pos=wx.Point(544, 16), size=wx.Size(51, 16), style=0)

        self.button_enable = wx.Button(id=wxID_PANELPRODUCTCUSTHISTORYBUTTON_ENABLE,
              label=u'\u5f00\u59cb\u7f16\u8f91', name=u'button_enable',
              parent=self, pos=wx.Point(600, 40), size=wx.Size(93, 26),
              style=0)
        self.button_enable.Bind(wx.EVT_BUTTON, self.OnButton_enableButton,
              id=wxID_PANELPRODUCTCUSTHISTORYBUTTON_ENABLE)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.lcSaleDetail = MyListCtrlProSaleDetail(self)
        self.lcSaleDetail.SetSize(wx.Size(1000, 450))
        self.lcSaleDetail.Move(wx.Point(10, 100))
        
    def setData(self, proId, peerId):
        proObj = self.m_pro = CDataProduct.findObj(proId)
        
        self.tcName.SetValue(proObj.getValue(proObj.COL_NAME))
        self.tcSpec.SetValue(proObj.getValue(proObj.COL_SPEC))
        self.TCPosition.SetValue(proObj.getValue(proObj.COL_POSITION))
        self.TCMemo.SetValue(proObj.getValue(proObj.COL_MEMO))
        
        self.m_pro.refreshOnhand()
        self.resetOnhand()
        self.resetManualNum()
        
        #self.lcSaleDetail.refresh({'proId':proId}) 
        self.lcSaleDetail.refresh({'peerId':peerId,
                                'proId':proId})
        
        self.m_pro = proObj
        
        self.tcName.Enable(False)
        self.tcSpec.Enable(False)
        self.TCPosition.Enable(False)
        self.TCMemo.Enable(False)
        self.btn_save.Enable(False)

    def resetOnhand(self):
        proObj = self.m_pro
        onhand = u'%g' %(proObj.getValue(proObj.COL_ONHAND1))
        self.tcOnhand.SetValue(onhand)
        
    def resetManualNum(self):
        proObj = self.m_pro
        manualNum = proObj.getValue(proObj.COL_MANUALNUM)
        if manualNum:
            self.tcManualNum.SetValue(manualNum)
        
    def OnBtnModifyOnhandButton(self, event):
        onhand = self.m_pro.getValue(self.m_pro.COL_ONHAND1)
        
        dlg = wx.TextEntryDialog(self, u'修改库存：', u'修改库存：', str(onhand))
        if (dlg.ShowModal() == wx.ID_OK):
            #self.m_pro.setValue(self.m_pro.COL_ONHAND1, float(dlg.GetValue()))
            #self.m_pro.update()
            self.m_pro.updateQuantity(float(dlg.GetValue()))
            
        self.resetOnhand()

    def OnBtnManualNumButton(self, event):
        manualNum = self.m_pro.getValue(self.m_pro.COL_MANUALNUM)
        if not manualNum:
            manualNum = ''
            
        dlg = wx.TextEntryDialog(self, u'修改手工编号：', u'修改手工编号：', manualNum)
        if (dlg.ShowModal() == wx.ID_OK):
            self.m_pro.setValue(self.m_pro.COL_MANUALNUM, dlg.GetValue())
            self.m_pro.update()
            
        self.resetManualNum()


    def OnBtn_saveButton(self, event):
        
        self.m_pro.setValue(self.m_pro.COL_NAME, self.tcName.GetValue())
        self.m_pro.setValue(self.m_pro.COL_SPEC, self.tcSpec.GetValue())
        self.m_pro.setValue(self.m_pro.COL_POSITION, self.TCPosition.GetValue())
        self.m_pro.setValue(self.m_pro.COL_MEMO, self.TCMemo.GetValue())
        self.m_pro.update()
        
        self.m_pro.updateQuantity(float(self.tcOnhand.GetValue()))
        self.resetOnhand()
        
        # 更新缓存
        obj = CDataProduct.getInstance({'id':self.m_pro.getKeyVal()})
        obj.setValue(obj.COL_NAME, self.tcName.GetValue())
        obj.setValue(obj.COL_SPEC, self.tcSpec.GetValue())
        obj.setValue(obj.COL_POSITION, self.TCPosition.GetValue())
        obj.setValue(obj.COL_MEMO, self.TCMemo.GetValue())

        self.tcName.Enable(False)
        self.tcSpec.Enable(False)
        self.TCPosition.Enable(False)
        self.TCMemo.Enable(False)
        self.btn_save.Enable(False)
        
    def OnButton_enableButton(self, event):
    
        self.tcName.Enable(True)
        self.tcSpec.Enable(True)
        self.TCPosition.Enable(True)
        self.TCMemo.Enable(True)
        self.btn_save.Enable(True)
